package com.funtonow.jqdk.ui;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.funtonow.jqdk.R;
import com.funtonow.jqdk.base.BaseActivity;
import com.funtonow.jqdk.base.Constants;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * @author Lenovo
 */
public class ShareActivity extends BaseActivity implements View.OnClickListener {

    private IWXAPI api;
    private TextView tv_share_wx;
    private TextView tv_share_pyq;
    private TextView tv_pop_close;

    private String title ;
    private String url ;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_share;
    }

    @Override
    public void onCreateActivity(@Nullable Bundle savedInstanceState) {
        String shareInfo = getIntent().getStringExtra("shareInfo");
        JSONObject jsonObject = JSON.parseObject(shareInfo);
        title = jsonObject.getString("name");
        url = jsonObject.getString("url");
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        getWindow().setAttributes(lp);

        tv_share_wx = (TextView) findViewById(R.id.tv_share_wx);
        tv_share_pyq = (TextView) findViewById(R.id.tv_share_pyq);
        tv_pop_close = (TextView) findViewById(R.id.tv_pop_close);
        tv_share_wx.setOnClickListener(this);
        tv_share_pyq.setOnClickListener(this);
        tv_pop_close.setOnClickListener(this);
    }

    public void shareWx(int flag) {
        api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true);
        api.registerApp(Constants.APP_ID);
        if (api.isWXAppInstalled()) {
            WXWebpageObject webpage = new WXWebpageObject();
            webpage.webpageUrl = url;
            WXMediaMessage msg = new WXMediaMessage(webpage);
            msg.title = title;
            msg.description = "";
            //这里替换一张自己工程里的图片资源
            Bitmap thumb = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
            msg.setThumbImage(thumb);

            SendMessageToWX.Req req = new SendMessageToWX.Req();
            req.transaction = String.valueOf(System.currentTimeMillis());
            req.message = msg;
            req.scene = flag == 0 ? SendMessageToWX.Req.WXSceneSession : SendMessageToWX.Req.WXSceneTimeline;
            api.sendReq(req);
            finish();
        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_share_wx:
                shareWx(0);
                break;
            case R.id.tv_share_pyq:
                shareWx(1);
                break;
            case R.id.tv_pop_close:
                finish();
                break;
            default:
                break;
        }
    }
}
